package com.wx.media;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wx.token.AccessToken;
import com.wx.type.MediaType;
import com.wx.util.Constants;
import com.wx.util.HttpUtil;
import com.wx.util.TokenUtil;

/**
 * 永久素材服务
 * 
 * @author su
 * 
 */
public class PermanentMediaService {
	private static Logger log = LoggerFactory.getLogger(PermanentMediaService.class);
	/**
	 * 上传图片
	 * @param access_token
	 * @param fileStream
	 * @return
	 */
	public String uploadimg(String access_token, String file) {
		String url = Constants.UPLOADIMG_MEDIA_POST_URL.replace("ACCESS_TOKEN",access_token);
		String res = "";
		try {
			res = HttpUtil.upload(url, file,null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.debug(res);
		return res;
	}
	
	/**
	 * 通过POST表单来调用接口，表单id为media，包含需要上传的素材内容，有filename、filelength、content-type等信息。请注意：图片素材将进入公众平台官网素材管理模块中的默认分组。
	 * @param access_token
	 * @param file
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public String add_material(String access_token, String file,Map params) throws Exception{
		String url = Constants.ADD_MATERIAL_MEDIA_POST_URL.replace("ACCESS_TOKEN",access_token);
		String res = "";
		try {
			res = HttpUtil.upload(url,file, params);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.debug(res);
		return res;
	}
	
	/**
	 * 1、新增的永久素材也可以在公众平台官网素材管理模块中看到
	2、永久素材的数量是有上限的，请谨慎新增。图文消息素材和图片素材的上限为5000，其他类型为1000
	3、素材的格式大小等要求与公众平台官网一致。具体是，图片大小不超过2M，支持bmp/png/jpeg/jpg/gif格式，语音大小不超过5M，长度不超过60秒，支持mp3/wma/wav/amr格式
	4、调用该接口需https协议
	 * @param access_token
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public String add_news_media(String access_token, String json) throws Exception{
		String url = Constants.UPLOAD_NEWS_MEDIA_POST_URL.replace("ACCESS_TOKEN",access_token);
		String res = "";
		try {
			res = HttpUtil.post(url,json);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.debug(res);
		return res;
	}
	
	/**
	 * 获取永久素材
	 * @param access_token
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public String get_media(String access_token, String json) throws Exception{
		String url = Constants.GET_MEDIA_POST_URL.replace("ACCESS_TOKEN",access_token);
		String res = "";
		try {
			res = HttpUtil.post(url,json);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.debug(res);
		return res;
	}
	
	/**
	 * 删除永久素材
	 * @param access_token
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public String del_media(String access_token, String json) throws Exception{
		String url = Constants.DEL_MEDIA_POST_URL.replace("ACCESS_TOKEN",access_token);
		String res = "";
		try {
			res = HttpUtil.post(url,json);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.debug(res);
		return res;
	}
	
	/**
	 * 获取素材总数
	 * 1.永久素材的总数，也会计算公众平台官网素材管理中的素材
	2.图片和图文消息素材（包括单图文和多图文）的总数上限为5000，其他素材的总数上限为1000
	3.调用该接口需https协议
	 * @param access_token
	 * @return
	 * @throws Exception
	 */
	public String get_media_count(String access_token) throws Exception{
		String url = Constants.GET_MEDIA_COUNT_GET_URL.replace("ACCESS_TOKEN",access_token);
		String res = "";
		try {
			res = HttpUtil.get(url);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.debug(res);
		return res;
	}
	
	/**
	 * 修改永久图文素材
	 * @param access_token
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public String update_news_media(String access_token, String json) throws Exception{
		/*{
			  "media_id":MEDIA_ID,
			  "index":INDEX,
			  "articles": {
			       "title": TITLE,
			       "thumb_media_id": THUMB_MEDIA_ID,
			       "author": AUTHOR,
			       "digest": DIGEST,
			       "show_cover_pic": SHOW_COVER_PIC(0 / 1),
			       "content": CONTENT,
			       "content_source_url": CONTENT_SOURCE_URL
			    }
			}*/
		return null;
	}
	
	public static void main(String[] args) throws Exception {
		AccessToken accessToken = TokenUtil.getAccessToken();
		String access_token = accessToken.getAccess_token();
//		String access_token = "5X7NKnajNBQCIpHTt9YAQ-3K2PLnXc-A3T9Ep8Bi01l2DGJdrLoF-wyzVsYhd6aocf9K6EWnFwm-G5zCfzeYznQwagXIi90K2RwOL4pU_3YKXHhAGATGQ";
		
		PermanentMediaService pm = new PermanentMediaService();
		ObjectMapper mapper = new ObjectMapper();
		//上传
		/*String imageFile = "D:/workspace/infoboard/src/chongwu1124.jpg";
		String res = pm.uploadimg(access_token,imageFile);
		
		String url = (String) mapper.readValue(res, Map.class).get("url");
//		System.out.println(url);
		Map<String,String> map = mapper.readValue(res, Map.class);
		String thumb_media_id = map.get("media_id");*/
		
		//新增其他类型永久素材
		String vidoe = "D:/workspace/infoboard/src/QQ图片20151127152941.gif";
		Map params = new HashMap();
		params.put("media", vidoe);
		params.put("type", MediaType.IMAGE);
		String res = pm.add_material(access_token,vidoe, params);
		System.out.println(res);
		Map<String,String> map = mapper.readValue(res, Map.class);
		String thumb_media_id = map.get("media_id");
		
		//新增永久图文素材
		
		Media media = new Media();
		Article article = new Article();
		article.setTitle("标题");
		article.setThumb_media_id(thumb_media_id);
		article.setAuthor("作者");
		article.setDigest("图文消息的摘要，仅有单图文消息才有摘要，多图文此处为空");
		article.setShow_cover_pic("1");
		article.setContent("图文消息的具体内容，支持HTML标签，必须少于2万字符，小于1M，且此处会去除JS");
		article.setContent_source_url("http://www.baidu.com");
		
		List list = new ArrayList();
		list.add(article);
		media.setArticles(list);
		
		String json = mapper.writeValueAsString(media);
		System.out.println(json);
		
		res = pm.add_news_media(access_token,json);
		
		//获取永久素材
		/*String json = "{\"media_id\":\"ahXlJT5o2dl5I-iIfixDG71q7fsAsFP5NMdFr0VeXoA\"}";
		String res = pm.get_media(access_token, json);*/
		
		//删除永久素材
		/*String res = pm.del_media(access_token, json);*/
		
		//获取素材总数  {"voice_count":0,"video_count":0,"image_count":6,"news_count":1}
//		String res = pm.get_media_count(access_token);
		
		System.out.println(res);
	}
}
